﻿using System;
using System.Threading.Tasks;

namespace Dora.Tools.Utility.Redis
{
    public class GenerateVerifyCode
    {
        private const string key = "_verifty";

        /// <summary>
        /// 生成随机验证码
        /// </summary>
        /// <param name="mobile">手机号码</param>
        /// <returns></returns>
        public static string CreateVerifyCode(string mobile)
        {
            if (!string.IsNullOrEmpty(mobile))
            {
                Random rad = new Random();
                int mobile_code = rad.Next(1000, 10000);
                RedisManager.Set(mobile + key, mobile_code, 600); // 10分钟
                return mobile_code.ToString();
            }

            return string.Empty;
        }
        #region OpenId=》验证码
        /// <summary>
        /// 创建OpenId的验证码
        /// </summary>
        /// <param name="mobile"></param>
        /// <returns></returns>
        public async static Task<string> OpenIdVerifyCode(string OpenId)
        {
            if (!string.IsNullOrEmpty(OpenId))
            {
                Random rad = new Random();
                int wx_code = rad.Next(100000, 999999);
                await RedisManager.SetAsync(OpenId, wx_code, 300); // 60/1分钟
                return wx_code.ToString();
            }
            return string.Empty;
        }
        /// <summary>
        /// 获取生成的OpenId验证码
        /// </summary>
        /// <param name="mobile"></param>
        /// <returns></returns>
        public static string GetOpenIdVerifyCode(string OpenId)
        {
            if (!string.IsNullOrEmpty(OpenId))
            {
                var wx_code = RedisManager.Get(OpenId);
                return wx_code;
            }
            return string.Empty;
        }
        #endregion
        /// <summary>
        /// 根据手机号码获取redis的key
        /// </summary>
        /// <param name="mobile">手机号码</param>
        /// <returns></returns>
        public static string GetVerifyKey(string mobile)
        {
            if (!string.IsNullOrEmpty(mobile))
            {
                return mobile + key;
            }

            return string.Empty;
        }

        /// <summary>
        /// 获取生成的随机验证码
        /// </summary>
        /// <param name="mobile">手机号码</param>
        /// <returns></returns>
        public static string GetVerifyCode(string mobile)
        {
            if (!string.IsNullOrEmpty(mobile))
            {
                var redis_key = mobile + key;
                var mobile_code=  RedisManager.Get(redis_key);
                return mobile_code;
            }

            return string.Empty;
        }
        /// <summary>
        /// 移除生成的随机验证码
        /// </summary>
        /// <param name="mobile">手机号码</param>
        /// <returns></returns>
        public static bool RemoveVerifyCode(string mobile)
        {
            if (!string.IsNullOrEmpty(mobile))
            {
                var redis_key = mobile + key;
                if (RedisManager.Exists(redis_key))
                {
                    return RedisManager.Del(redis_key)>0;
                }
            }

            return false;
        }
        /// <summary>
        /// 移除微信生成的随机验证码
        /// </summary>
        /// <param name="mobile">手机号码</param>
        /// <returns></returns>
        public static bool RemoveWxVerifyCode(string OpenID)
        {
            if (!string.IsNullOrEmpty(OpenID))
            {
                var redis_key =OpenID;
                if (RedisManager.Exists(redis_key))
                {
                    return RedisManager.Del(redis_key) > 0;
                }
            }
            return false;
        }
    }
}
